﻿using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SshNet;
using System.IO;

namespace SSHTool
{

    public class SSHTooltest
    {

        private readonly string _host;
        private readonly string _username;
        private readonly string _password;
        private SshClient _client;
        private ShellStream _shellStream;
        private StreamReader _reader;

        public SSHTooltest(string host, string username, string password)
        {
            _host = host;
            _username = username;
            _password = password;
        }
        public bool Connect()
        {
          
            _client = new SshClient(_host, _username, _password);
            try
            {
                _client.Connect();
                _shellStream = _client.CreateShellStream("SSH Tool", 80, 24, 800, 600, 1024);
                _reader = new StreamReader(_shellStream);
                return _client.IsConnected;
            }
            catch (Exception ex)
            {
                
                return false;
            }
        }

        public void Disconnect()
        {
            _reader?.Dispose();
            if (_client != null && _client.IsConnected)
            {
                _client.Disconnect();
            }
        }

        public string SendCommand(string command)
        {
            if (_client != null && _client.IsConnected)
            {
                var cmd = _client.CreateCommand(command);
                
                return cmd.Execute();
            }

            throw new InvalidOperationException("SSH client is not connected.");
        }


        public void SendCommandByStream(string command)
        {
            if (_shellStream != null)
            {
                _shellStream.WriteLine(command);
                _shellStream.Flush();
            }
        }
        public string ReadResponse()
        {
            if (_shellStream != null && _shellStream.DataAvailable)
            {
                return _reader.ReadToEnd();
            }
            return null;
        }

        // 这是一个循环方法，持续从SSH连接读取数据
        public string ContinuousRead()
        {
            while (true)
            {
                if (_shellStream.DataAvailable)
                {
                    var data = ReadResponse();
                    if (!string.IsNullOrEmpty(data))
                    {
                       return(data);
                    }
                }
                System.Threading.Thread.Sleep(200); // 每0.5秒查询一次是否有新数据
            }
        }


    }
}



// 使用示例：
/*
var ssh = new SSHTool("your_host_ip", "your_username", "your_password");
if (ssh.Connect())
{
    string result = ssh.SendCommand("show version"); // 用你需要的命令替换
    Console.WriteLine(result);
    ssh.Disconnect();
}
else
{
    Console.WriteLine("Failed to connect.");
}
*/






// 使用示例：
/*
var ssh = new SSHTool("your_host_ip", "your_username", "your_password");
if (ssh.Connect())
{
    ssh.SendCommand("show logging"); // 用你需要的命令替换
    ssh.ContinuousRead(); // 这会开始一个无限循环，持续读取数据
}
else
{
    Console.WriteLine("Failed to connect.");
}
*/
